package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.ITravelCommendService;
import cn.wolfcode.luowowo.cache.key.RedisKey;
import cn.wolfcode.luowowo.cache.service.IUserInfoCacheService;
import cn.wolfcode.luowowo.cache.service.IVerifyCodeCacheService;
import cn.wolfcode.luowowo.common.query.QueryObject;
import cn.wolfcode.luowowo.common.query.StrategyQuery;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.common.util.Consts;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.website.annotation.LoginRequired;
import com.alibaba.fastjson.JSON;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.UUID;

@Controller
public class LoginController {
    @Reference
    private IUserInfoService userInfoService;
    @Reference
    private IVerifyCodeCacheService verifyCodeCacheService;
    @Reference
    private IUserInfoCacheService userInfoCacheService;
    @Reference
    private IStrategyCommendService strategyCommendService;
    @Reference
    private ITravelCommendService travelCommendService;

    @RequestMapping("/userLogin")
    @ResponseBody
    public Object userLogin(String phone, String password, HttpSession session, HttpServletResponse response) {
        AjaxResult ajaxResult = userInfoService.userLogin(phone, password);
        if (ajaxResult.isSuccess()) {
            //成功就存到session
            session.setAttribute(Consts.USER_INFO, ajaxResult.getData());
            //存到redis
            String json = JSON.toJSONString(ajaxResult.getData());
            String postfix = UUID.randomUUID().toString().replaceAll("-", "");
            String token = RedisKey.USER_INFO.getCacheKey(postfix);
            userInfoCacheService.setUserInfo(token, json);
            //将token以cookie返回
            Cookie cookie = new Cookie(Consts.USER_INFO_TOKEN, token);
            cookie.setPath("/");
            response.addCookie(cookie);
        }
        return ajaxResult;
    }

    @RequestMapping("/checkPhone")
    @ResponseBody
    public boolean checkPhone(String phone) {
        return userInfoService.checkPhone(phone);
    }

    @RequestMapping("/sendVerifyCode")
    @ResponseBody
    public Object sendVerifyCode(String phone) {
        String verifyCode = UUID.randomUUID().toString().substring(0, 4);
        System.out.println(verifyCode);
        return verifyCodeCacheService.setVerifyCodeCache(phone, verifyCode);
    }

    @RequestMapping("/userRegist")
    @ResponseBody
    public Object userRegist(String phone, String nickname, String password, String rpassword, String verifyCode) {
        return userInfoService.userRegist(phone, nickname, password, rpassword, verifyCode);
    }

}
